<?php

namespace admin\controllers;

use yii;
use yii\web\Controller;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;

use admin\models\UploadForm;
use yii\web\UploadedFile;


class ServerController extends Controller {

    public $enableCsrfValidation = false;
    
    /**
     * 图片上传
     */
    public function actionUploadImages()
    {
        $upload = new UploadForm();
        if (Yii::$app->request->isPost) { 
            $file = UploadedFile::getInstance($upload, 'file');  
            if(!$file){
                echo json_encode(array('status'=> 3,'msg'=>'上传失败！'));
                exit;
            }
            $basePath = yii\helpers\Url::to('@app');
            $base_url = Yii::$app->request->hostInfo;
            $filePath = $basePath.'/web/uploads/pic/'.date("Ymd",time());
            if (!file_exists($filePath)){
                if(!mkdir($filePath,0777,true))
                {
                    echo json_encode(array('status'=> 1,'msg'=>'上传失败！'));
                    exit;
                }
            }
            if ($file && $upload->validate()) { 
                $file_name = date('Ymd') .$file->baseName. '.' . $file->extension;
                $path = $filePath.'/'.$file_name;
                $res = $file->saveAs($path);
                $result = [
                    'name' => '',
                    'url'  => '',
                ];
                if($res){
                    $result['name'] = $file_name;
                    $result['url'] = $base_url."/uploads/pic/".date("Ymd",time()).'/'.$file_name;
                }
                if($result['url'] != ''){
                    echo json_encode(array( 'status' => 0 , 'msg' => '成功' , 'data' => $result ));
                    exit;  
                }else{
                    echo json_encode(array( 'status' => 2 , 'msg' => '上传失败！' ));
                    exit;
                }
            }  
        }
    }
    
    /**
     * 音频上传
     */
    public function actionUploadAudio()
    {
        $upload = new UploadForm();
        if (Yii::$app->request->isPost) { 
            $file = UploadedFile::getInstance($upload, 'file');  
            if(!$file){
                echo json_encode(array('status'=> 3,'msg'=>'上传失败！'));
                exit;
            }
            $basePath = yii\helpers\Url::to('@app');
            $base_url = Yii::$app->request->hostInfo;
            $filePath = $basePath.'/web/uploads/audio/'.date("Ymd",time());
            if (!file_exists($filePath)){
                if(!mkdir($filePath,0777,true))
                {
                    echo json_encode(array('status'=> 1,'msg'=>'上传失败！'));
                    exit;
                }
            }
            if ($file && $upload->validate()) { 
                $file_name = date('Ymd') .$file->baseName. '.' . $file->extension;
                $path = $filePath.'/'.$file_name;
                $res = $file->saveAs($path);
                $result = [
                    'name' => '',
                    'url'  => '',
                ];
                if($res){
                    $result['name'] = $file_name;
                    $result['url'] = $base_url."/uploads/audio/".date("Ymd",time()).'/'.$file_name;
                }
                if($result['url'] != ''){
                    echo json_encode(array( 'status' => 0 , 'msg' => '成功' , 'data' => $result ));
                    exit;  
                }else{
                    echo json_encode(array( 'status' => 2 , 'msg' => '上传失败！' ));
                    exit;
                }
            }  
        }
    }
    
    /**
     * 视频上传
     */
    public function actionUploadVideo()
    {
        $upload = new UploadForm();
        if (Yii::$app->request->isPost) { 
            $file = UploadedFile::getInstance($upload, 'file');  
            if(!$file){
                echo json_encode(array('status'=> 3,'msg'=>'上传失败！'));
                exit;
            }
            $basePath = yii\helpers\Url::to('@app');
            $base_url = Yii::$app->request->hostInfo;
            $filePath = $basePath.'/web/uploads/video/'.date("Ymd",time());
            if (!file_exists($filePath)){
                if(!mkdir($filePath,0777,true))
                {
                    echo json_encode(array('status'=> 1,'msg'=>'上传失败！'));
                    exit;
                }
            }
            if ($file && $upload->validate()) { 
                $file_name = date('Ymd') .$file->baseName. '.' . $file->extension;
                $path = $filePath.'/'.$file_name;
                $res = $file->saveAs($path);
                $result = [
                    'name' => '',
                    'url'  => '',
                ];
                if($res){
                    $result['name'] = $file_name;
                    $result['url'] = $base_url."/uploads/video/".date("Ymd",time()).'/'.$file_name;
                }
                if($result['url'] != ''){
                    echo json_encode(array( 'status' => 0 , 'msg' => '成功' , 'data' => $result ));
                    exit;  
                }else{
                    echo json_encode(array( 'status' => 2 , 'msg' => '上传失败！' ));
                    exit;
                }
            }  
        }
    }

}
